/*
 * @(#)BaseBean.java            2004. 12. 14.
 *
 * Copyright (c) 1998-2004 Amail, Inc.
 * 708-8 Global Building 10th floor, YeokSamdong, Gangnamgu, Seoul, 
 * Korea republic of. All rights reserved.
 * 
 * This software is the confidential and proprietary information of Amail,
 * Inc. ("Confidential Information"). You shall not disclose such 
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into Amail.
 * 
 */
 
 
package sample;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import sample.Locale;
/**
 * Class description :
 * 
 * @version		
 * @author 		lena
 *
 */
public class BaseBean {
	
	
	/**
	 * Retrieves the String value of the field, with value 
	 * encoded with the rules in Locale.java. Getter method of
	 * the field should be defined and declared as public. Also
	 * you must initialize Locale.java before using it.
	 * @param field name of the field
	 * @return
	 */
	public final String get(String field) {
	
		if( field == null || field.trim().length() < 1 )
			return null;
		
		try {
			String methodName =
					"get" 
					+ field.substring(0,1).toUpperCase() 
					+ field.substring(1);
		
			Method targetMethod = 
					this.getClass().getDeclaredMethod(methodName,null);
			Object returnObject = targetMethod.invoke(this,null);
		
			return Locale.fromWeb(
					returnObject==null?null:returnObject.toString());
		} catch(Exception ex) {
			return null;
		}
	}

	/**
	 * <br>field��媛믪쓣 �뗮똿�쒕떎. 媛믪� set[field紐� 硫붿냼�쒕� �댁슜�댁꽌 �ｌ쑝硫�
	 * <br>web character濡�蹂�솚�쒕떎. 留뚯빟 �꾨뱶紐낆뿉 �대떦�섎뒗 set硫붿냼�쒓� 
	 * <br>�녿뒗 寃쎌슦�먮뒗 �꾨Т�쇰룄 �섏� �딅뒗�� 
	 * @param field
	 * @param value
	 */
	public final void set(String field, String value) {

		if( field == null || field.trim().length() < 1 ) return;
	
		try {
			String methodName =
					"set" 
					+ field.substring(0,1).toUpperCase() 
					+ field.substring(1);
		
			Class[] methodArgs = new Class[1];
			methodArgs[0] = String.class;
		
			Method targetMethod = null;
		
			String[] args = new String[1];
			args[0] = Locale.toWeb(value);
		
			try {
				targetMethod = this.getClass().getDeclaredMethod(methodName,methodArgs);
				targetMethod.invoke(this,args);
			} catch ( Exception ex2) {
				// BaseActionForm�먯꽌 inherited��method���덈뒗吏�寃�궗�대낯��
//				targetMethod = (new BaseActionForm()).getClass().getDeclaredMethod(methodName,methodArgs);
			}
			targetMethod.invoke(this,args);
		} 
		catch(Exception ex) {}
	}
	
	/**
	 * Retrieves the string containing the information of fields 
	 * and their values got with their getter methods. Only public
	 * fields are extracted.
	 * @return information string
	 */
	public String getDebugString() {
	
		StringBuffer sb = new StringBuffer();
	
		Class self = this.getClass();
	
		Field[] selfFields = self.getFields();
	
		try {
			String fieldName;
			String methodName;
			Method targetMethod;
			Object returnedObject;
		
			for (int u = 0; u < selfFields.length; u++) {
				fieldName = selfFields[u].getName();
			
				sb.append(fieldName);
				sb.append(" = ");
			
				methodName =
						"get" 
						+ fieldName.substring(0,1).toUpperCase() 
						+ fieldName.substring(1);
				targetMethod = self.getDeclaredMethod(methodName,null);
				returnedObject = targetMethod.invoke(this,null);
							
				sb.append( 
						(returnedObject == null)
						? "null"
						: returnedObject.toString() );
				sb.append("\r\n");
			}
		} catch(Exception e) {
			sb.append(e.toString());
		}
		return sb.toString(); 
	}
	public final int getInt(String field){
		return Integer.parseInt(get(field));
	}
	
	public final float getFloat(String field){
		return Float.parseFloat(get(field));
	}
	
	public final double getDoulbe(String field){
		return Double.parseDouble(get(field));
	}
	
}
